package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.RoleService;

import com.itheima.health.service.UserListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userItem")
@Slf4j
public class UserListController {



    /**
     * @author zhangmeng
     * @description 检查项Controller
     * @date 2019/9/16
     **/


        @Reference
        private UserListService userListService;

        @Reference

        private RoleService roleService;

        /**
         * 新增
         * 1、rpc调用完成新增业务
         * 2、返回结果
         *
         * @param
         * @return
         */
        @PostMapping("/add")
        public Result add(@RequestBody User user,Integer[] roleIds){
            log.info("[检查项-新增]data:{}",user);
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userListService.add(user,roleIds);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        }

        /**
         * 分页查询
         *
         * @param queryPageBean
         * @return
         */
        @GetMapping("findPage")
      
        public Result findPage(QueryPageBean queryPageBean) {
            log.info("[检查项-分页查询]data:{}", queryPageBean);
            //rpc查询数据
            PageResult pageResult = userListService.pageQuery(queryPageBean);
            //封装返回结果
            return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
        }

        /**
         * 根据id删除
         *
         * @param id
         * @return
         */
        @RequestMapping("/delete")
     
        public Result delete(Integer id){
            log.info("[检查项-根据id删除]id：{}",id);
            //RPC调用处理业务
            userListService.delete(id);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }

        /**
         * 编辑
         *
         * @param
         * @return
         */
        @RequestMapping("/edit")
      
        public Result edit(@RequestBody User user,Integer[] roleIds) {
            log.info("[检查项-编辑]data:", user);
            userListService.edit(user,roleIds);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        }

        /**
         * 根据Id查询
         *
         * @param id
         * @return
         */
        @RequestMapping("/findById")
       
        public Result findById(Integer id) {
            log.info("[检查项-根据ID查询]id:{}", id);
            User User = userListService.findById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,User);
        }

        /**
         * 查询所有检查项
         * @return
         */
        @RequestMapping("/RolefindAll")
      
        public Result findAll(){

            List<Role> Users = roleService.findAll();
            return new Result(true,MessageConst.ACTION_SUCCESS,Users);
        }


        @RequestMapping("/findRoleIdsByUserId")
        public Result findRoleIdsByUserId(Integer id){
            List<Integer> roleIdsByUserIds = userListService.findRoleIdsByUserIds(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,roleIdsByUserIds);
        }




}
